﻿using System;
using System.Globalization;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class AddNewConsultant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
        }

        protected void ButtonAddNewClick(object sender, EventArgs e)
        {
            if (checkAutoAccount.Checked)
            {
                var userName = AdministrationUtilityLogic.AutoUserName(textFullName.Text);
                // Add new account for contractor
                var accountEntity = new AccountEntity
                                        {
                                            UserName = userName,
                                            Password = "123456",
                                            // Default password
                                            RoleId = (int) RoleList.Consultant,
                                            Status = 1,
                                            ActorType = "Consultant"
                                        };
                var result = AdministrationUtilityLogic.AddNewAccount(accountEntity);

                var eConsultant = new ConsultantEntity
                                      {
                                          UserName = userName,
                                          FullName = Server.HtmlEncode(textFullName.Text),
                                          Email = Server.HtmlEncode(textEmail.Text),
                                          CompanyName = Server.HtmlEncode(textCompanyName.Text),
                                          PhoneNumber = Server.HtmlEncode(textPhoneNumber.Text),
                                          Address = Server.HtmlEncode(textAddress.Text)
                                      };

                var insert = ConsultantProfileLogic.InsertConsultants(eConsultant);

                if (result == "Successfully!" && insert == "Successfully!")
                {
                    Response.Redirect("ListConsultant.aspx");
                }
                labelStatus.Text = insert;
                UserEventEntity(insert);
            }
            else
            {
                var eConsultant = new ConsultantEntity
                                      {
                                          UserName = "",
                                          FullName = textFullName.Text,
                                          Email = textEmail.Text,
                                          CompanyName = textCompanyName.Text,
                                          PhoneNumber = textPhoneNumber.Text,
                                          Address = textAddress.Text
                                      };

                var insert = ConsultantProfileLogic.InsertConsultants(eConsultant);

                if (insert == "Successfully!")
                {
                    Response.Redirect("ListConsultant.aspx");
                }
                labelStatus.Text = insert;
                UserEventEntity(insert);
            }
        }

        protected void ButtonResetClick(object sender, EventArgs e)
        {
            textFullName.Text = string.Empty;
            textEmail.Text = string.Empty;
            textCompanyName.Text = string.Empty;
            textPhoneNumber.Text = string.Empty;
            textAddress.Text = string.Empty;
        }
        //event of insert client
        private void UserEventEntity(string userEvent)
        {
            // Save event of insert client
            var userEventEntity = new EventEntity
            {
                UserName = CommonLogic.ConvertToString(Session["UserName"]),
                EventType = (int)EventTypeList.UserEvent,
                OnDate = DateTime.Now,
                Description = userEvent,
            };
            EventTracer.SaveEvents(userEventEntity);
        }
        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.AddConsultant;

            if (Page.Master == null) return;
            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }
    }
}
